package com.canbot.u05.activity.presidemode.newcode;

import android.content.Intent;
import android.widget.Toast;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.ActionBean;
import com.canbot.u05.activity.presidemode.newcode.bean.NModuleType;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IHeadCommonClick;
import com.canbot.u05.activity.presidemode.newcode.view.HeadCommonView;
import com.canbot.u05.activity.presidemode.newcode.view.ItemChoiceView;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;

public class NActionChoiceSettingsActivity extends BaseHostActivity {
    @BindView(R.id.headCommonView)
    HeadCommonView headCommonView;
    @BindView(R.id.item_choice_random_action)
    ItemChoiceView itemChoiceRandomAction;
    @BindView(R.id.item_choice_existAction)
    ItemChoiceView itemChoiceExistAction;
    @BindView(R.id.item_choice_none_action)
    ItemChoiceView itemChoiceNoneAction;
    private int resultCode;
    private List<ActionBean> actionBeanList;
    private int actionType = NModuleType.ACTION_NONE;

    @Override
    protected void initEventData() {
        MapVoiceAndEyes.VoiceAndEyesInfo voiceTip = new MapVoiceAndEyes.VoiceAndEyesInfo(MapVoiceAndEyes.PRESENTER_VOICE_DIR + "jbkz_dzxz_u05_001.wav", "26");
        MapVoiceAndEyes.playMapVoiceAndEyes(voiceTip);
        initCommonHead();
        itemChoiceExistAction.setOnClickListener(v -> {
            gotoActivityForResult(NChoiceActionActivity.class,
                    NModuleType.ACTION_EXIST, actionType == NModuleType.ACTION_EXIST ? (Serializable) actionBeanList : null);
        });
        itemChoiceRandomAction.setOnClickListener(v -> {
            actionBeanList = null;
            itemChoiceNoneAction.cancle();
            itemChoiceExistAction.cancle();
            itemChoiceRandomAction.select();
        });
        itemChoiceNoneAction.setOnClickListener(v -> {
            actionBeanList = null;
            itemChoiceExistAction.cancle();
            itemChoiceRandomAction.cancle();
            itemChoiceNoneAction.select();
        });
        initIntent();
    }

    private void initCommonHead() {
        headCommonView.setiHeadCommonClick(pos -> {
            switch (pos) {
                case IHeadCommonClick.LEFT:
                    finish();
                    break;
                case IHeadCommonClick.RIGHT:
                    if (itemChoiceRandomAction.isSelect()) {
                        Intent intent = new Intent();
                        ActionBean actionBean = new ActionBean();
                        actionBean.actionType = NModuleType.ACTION_RANDOM;
                        intent.putExtra(DATA, actionBean);
                        resultCode = NModuleType.ACTION_RANDOM;
                        setResult(resultCode, intent);
                        finish();
                    } else if (itemChoiceExistAction.isSelect()) {
                        if (actionBeanList == null || actionBeanList.size() == 0) {
                            Toast.makeText(this, getString(R.string.action_null_tip), Toast.LENGTH_SHORT).show();
                        } else {
                            Intent intent = new Intent();
                            intent.putExtra(DATA, (Serializable) actionBeanList);
                            setResult(resultCode, intent);
                            finish();
                        }
                    } else {
                        // Toast.makeText(this, getString(R.string.action_null_tip), Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent();
                        resultCode = NModuleType.ACTION_NONE;
                        setResult(resultCode, intent);
                        finish();
                    }
                    break;
            }
        });
    }

    private void initIntent() {
        actionBeanList = (List<ActionBean>) getIntent().getSerializableExtra(DATA);
        if (actionBeanList != null && actionBeanList.size() > 0) {
            ActionBean actionBean = actionBeanList.get(0);
            actionType = actionBean.actionType;
            if (NModuleType.ACTION_EXIST == actionType) {
                itemChoiceExistAction.select();
            } else if (NModuleType.ACTION_RANDOM == actionType) {
                itemChoiceRandomAction.select();
            } else {
                itemChoiceNoneAction.select();
            }
        } else {
            itemChoiceNoneAction.select();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            if (requestCode == NModuleType.ACTION_EXIST) {
                actionBeanList = (List<ActionBean>) data.getSerializableExtra(DATA);
                if (actionBeanList != null && actionBeanList.size() > 0) {
                    itemChoiceNoneAction.cancle();
                    itemChoiceRandomAction.cancle();
                    itemChoiceExistAction.select();
                    this.resultCode = NModuleType.ACTION_EXIST;
                }
            }
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_naction_choice_settings;
    }

}
